package com.computergames.server.service.getCurrentPlayerStrategy;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class Bid_GetLandlordCurrentPlayer implements Bid_GetCurrentPlayerStrategy{
    private static final String[] PLAYERS = {"A", "B", "C"};
    private final Map<String, BidState> roomStates = new ConcurrentHashMap<>();

    @Override
    public boolean getCurrentPlayer(String roomName, String player, int bid) {
        BidState state = roomStates.computeIfAbsent(roomName, k -> new BidState());

        // 检查是否轮到该玩家叫牌
        if (!player.equals(PLAYERS[state.currentPlayerIndex.get()])) {
            return false;
        }

        // 检查叫牌分数
        if(!player.equals("A")){
            if (bid <= state.lastBid.get()) {
                return false;
            }
        }

        if (bid > 3 || bid < 0) {
            return false;
        }

        // 更新状态
        state.currentPlayerIndex.set((state.currentPlayerIndex.get() + 1) % PLAYERS.length);
        state.lastBid.set(bid);

        return true;
    }

    private static class BidState {
        AtomicInteger currentPlayerIndex = new AtomicInteger(0);
        AtomicInteger lastBid = new AtomicInteger(0);
    }
}
